package com.city.parking;

import java.lang.reflect.Array;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.city.parking.util.HttpResult;
import com.city.parking.util.SharePrefUtil;
import com.city.parking.util.Utils;

import android.os.Parcel;
import android.os.Parcelable;

public class LeaseItem implements Parcelable {
	public String name;
	public String province;
	public String city;
	public String district;
	public String street;
	public double lat, lng;
	public String contact;
	public String phone;
	public String date;
	public int duration;
	public String extra = "非诚勿扰";
	public boolean isOut = false;
	public double price = 0;

	private static final String UID = "uid";
	private static final String NAME = "name";
	private static final String PROVINCE = "province";
	private static final String CITY = "city";
	private static final String DISTRICT = "district";
	private static final String STREET = "street";
	private static final String LATITUDE = "latitude";
	private static final String LONGITUDE = "longitude";
	private static final String CONTACT = "contact";
	private static final String PHONE = "number";
	private static final String DATE = "date";
	private static final String DURATION = "tenancy";
	private static final String EXTRA = "description";
	private static final String PRICE = "price";
	private static final String ISOUT = "isout";

	public LeaseItem(String name, int price, String date) {
		this.name = name;
		this.price = price;
		this.date = date;
	}

	public LeaseItem(Parcel in) {
		name = in.readString();
		province = in.readString();
		city = in.readString();
		district = in.readString();
		street = in.readString();
		contact = in.readString();
		phone = in.readString();
		duration = in.readInt();
		extra = in.readString();
		lat = in.readDouble();
		lng = in.readDouble();
		price = in.readDouble();
		date = in.readString();
		isOut = (in.readInt() != 0);
	}

	public LeaseItem() {
	}

	public String getAddr() {
		return city + district + street;
	}

	public static LeaseItem fromJson(String str) {
		try {
			JSONObject json = new JSONObject(str);
			LeaseItem ret = new LeaseItem();
			if (json.has(NAME))
				ret.name = json.getString(NAME);
			if (json.has(PROVINCE))
				ret.province = json.getString(PROVINCE);
			if (json.has(CITY))
				ret.city = json.getString(CITY);
			if (json.has(DISTRICT))
				ret.district = json.getString(DISTRICT);
			if (json.has(STREET))
				ret.street = json.getString(STREET);
			if (json.has(CONTACT))
				ret.contact = json.getString(CONTACT);
			if (json.has(PHONE))
				ret.phone = json.getString(PHONE);
			if (json.has(DURATION))
				ret.duration = json.getInt(DURATION);
			if (json.has(PRICE))
				ret.price = json.getDouble(PRICE);
			if (json.has(LATITUDE))
				ret.lat = json.getDouble(LATITUDE);
			if (json.has(LONGITUDE))
				ret.lng = json.getDouble(LONGITUDE);
			if (json.has(DATE))
				ret.date = json.getString(DATE);
			if (json.has(ISOUT))
				ret.isOut = (json.getInt(ISOUT) != 0);
			return ret;
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return null;
	}
	
	public static ArrayList<LeaseItem> fromJsonArray(String str) {
		try {
			JSONArray ja = new JSONArray(str);
			ArrayList<LeaseItem> list = new ArrayList<LeaseItem>();
			for (int i=0; i<ja.length(); i++) {
				JSONObject o = ja.getJSONObject(i);
				LeaseItem item = fromJson(o.toString());
				if (item != null)
					list.add(item);
			}
			return list;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String toJsonStr() {
		JSONObject json = new JSONObject();
		try {
			json.put(UID, ParkingApp.mUid);
			json.put(NAME, name);
			json.put(ISOUT, isOut ? 1 : 0);
			json.put(PROVINCE, province);
			json.put(CITY, city);
			json.put(DISTRICT, district);
			json.put(STREET, street);
			json.put(CONTACT, contact);
			json.put(PHONE, phone);
			json.put(DURATION, duration);
			json.put(PRICE, price);
			json.put(LATITUDE, lat);
			json.put(LONGITUDE, lng);
			json.put(DATE, date);
			json.put(EXTRA, extra);
			return json.toString();
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		return null;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int arg1) {
		// TODO Auto-generated method stub
		out.writeString(name);
		out.writeString(province);
		out.writeString(city);
		out.writeString(district);
		out.writeString(street);
		out.writeString(contact);
		out.writeString(phone);
		out.writeInt(duration);
		out.writeString(extra);
		out.writeDouble(lat);
		out.writeDouble(lng);
		out.writeDouble(price);
		out.writeString(date);
		out.writeInt(isOut?1:0);
	}

	public static final Parcelable.Creator<LeaseItem> CREATOR = new Creator<LeaseItem>() {
        @Override
        public LeaseItem[] newArray(int size) {
            return new LeaseItem[size];
        }

        @Override
        public LeaseItem createFromParcel(Parcel source) {
            return new LeaseItem(source);
        }
    };
}
